package com.oa.lms.apps.business.notice.impl;

import java.util.Date;
import java.util.List;

import com.oa.lms.apps.business.notice.IMessageInfoService;
import com.oa.lms.apps.business.user.IUsersService;
import com.oa.lms.apps.constants.IDEDStatus;
import com.oa.lms.apps.dao.notice.IMessageInfoDao;
import com.oa.lms.apps.entity.notice.MessageInfoValue;

public class MessageInfoServiceImpl implements IMessageInfoService {

	

	private IMessageInfoDao messageInfoDao;
	
	private IUsersService usersService;
	
	private MessageInfoValue messageInfoValue;
	@Override
	public Integer add(String title, String contents, Date createTime,
			Integer userId) {
		messageInfoValue.setTitle(title);
		messageInfoValue.setContents(contents);
		messageInfoValue.setCreateTime(createTime);
		messageInfoValue.setUserItem(usersService.queryByUserId(userId));
		messageInfoValue.setStatus(IDEDStatus.DISABLE);
		return messageInfoDao.add(messageInfoValue);
	}

	@Override
	public void modify(Integer messageId, String title, String contents,
			Date createTime, Integer userId) {
		messageInfoValue = this.queryByMessageId(messageId);
		messageInfoValue.setTitle(title);
		messageInfoValue.setContents(contents);
		messageInfoValue.setCreateTime(createTime);
		messageInfoValue.setUserItem(usersService.queryByUserId(userId));
		messageInfoDao.modify(messageInfoValue);
	}
	
	@Override
	public void delete(Integer messageId) {
		messageInfoValue = this.queryByMessageId(messageId);
		messageInfoDao.delete(messageInfoValue);
	}
	
	@Override
	public void toRead(Integer messageId) {
		messageInfoValue = this.queryByMessageId(messageId);
		messageInfoValue.setStatus(IDEDStatus.ENABLE);
		messageInfoDao.modify(messageInfoValue);
	}

	@Override
	public MessageInfoValue queryByMessageId(Integer messageId) {
		return messageInfoDao.queryByMessageId(messageId);
	}

	@Override
	public List<MessageInfoValue> queryMessageInfo(Integer userId, Integer status, Integer rows, Integer pageNo) {
		if(userId != null)
			messageInfoValue.setUserItem(usersService.queryByUserId(userId));
		if(status != null)
			messageInfoValue.setStatus(status);
		
		return messageInfoDao.queryMessageInfo(messageInfoValue, rows, pageNo);
	}
	
	@Override
	public Integer getTotalPageNumber(Integer userId, Integer rows) {
		if(userId != null)
			messageInfoValue.setUserItem(usersService.queryByUserId(userId));
		
		return messageInfoDao.getTotalPageNumber(messageInfoValue, rows);
				
	}
	
	//Getter and Setter

	public IMessageInfoDao getMessageInfoDao() {
		return messageInfoDao;
	}

	public void setMessageInfoDao(IMessageInfoDao messageInfoDao) {
		this.messageInfoDao = messageInfoDao;
	}

	public IUsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}

	public MessageInfoValue getMessageInfoValue() {
		return messageInfoValue;
	}

	public void setMessageInfoValue(MessageInfoValue messageInfoValue) {
		this.messageInfoValue = messageInfoValue;
	}

}
